//
//  HttpReauestParam.m
//  HaierGMS_APP
//
//  Created by 堂堂 on 16/7/1.
//  Copyright © 2016年 堂堂. All rights reserved.
//

#import "HttpReauestParam.h"
#import<CommonCrypto/CommonDigest.h>
#import "Base64Until.h"
@implementation HttpReauestParam


- (id)initWithData:(NSString *)notify_type content:(NSString *)content urlType:(NSInteger)urlType
{
    self = [super init];
    if (self) {
        //218.58.70.212:9999
        NSString *url = @"http://218.58.70.212:9999/app-web/gateway/handle_app_web_request.json";
        NSString *url2 = @"http://218.58.70.212:9999/app-web/gateway/handle_app_web_request.json";
        self.param = [[NSMutableDictionary alloc]init];
        if(urlType == 0){
            self.SEVICE_URL = url;
        }else{
            self.SEVICE_URL = url2;
        }
            
        
        [self.param setObject:[[NSUUID alloc]init].UUIDString forKey:@"notify_id"];
        [self.param setObject:notify_type forKey:@"notify_type"];
        
        NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
        NSTimeInterval a = [dat timeIntervalSince1970] * 1000;
        NSString *timeString = [NSString stringWithFormat:@"%f", a];
        
        [self.param setObject:timeString forKey:@"notify_time"];
        [self.param setObject:@"json" forKey:@"data_type"];
        [self.param setObject:@"rrs_app" forKey:@"partner"];
        [self.param setObject:content forKey:@"content"];
        
        NSString *content1 = [NSString stringWithFormat:@"%@%@",content,@"2c94c4277f2742498ece1fea1f6d3998"];
       
        NSString * md5Str =[self md5:content1];
      
        NSString *sign = [Base64Until encode: md5Str];
        
        [self.param setObject:sign forKey: @"sign"];

        
    }
    return self;
}

-(NSString *) getPost{
    
    NSMutableString *pos = [[NSMutableString alloc]init];
    [pos appendFormat:@"%@?",self.SEVICE_URL];
    [pos appendFormat:@"%@=%@&",@"notify_id",[self.param objectForKey:@"notify_id"]];
    [pos appendFormat:@"%@=%@&",@"notify_type",[self.param objectForKey:@"notify_type"]];
    [pos appendFormat:@"%@=%@&",@"notify_time",[self.param objectForKey:@"notify_time"]];
    [pos appendFormat:@"%@=%@&",@"data_type",[self.param objectForKey:@"data_type"]];
    [pos appendFormat:@"%@=%@&",@"partner",[self.param objectForKey:@"partner"]];
    [pos appendFormat:@"%@=%@&",@"content",[self.param objectForKey:@"content"]];
    [pos appendFormat:@"%@=%@",@"sign",[self.param objectForKey:@"sign"]];

    return pos;
}
- (NSString *) md5:(NSString *) input {
    const char *cStr = [input UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call
    
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    
    return  output;
}


@end
